import { Tasks } from "@/global/creep-task";
import { assignPrototype } from "@/utils/functions/assignPrototype";

/**
 * Creep 有很多逻辑是相通的，这个扩展提取了相似的东西以减小代码复杂度。
 */
class CreepExtension extends Creep {

    collectEnergy(...from: AnyStoreStructure[]) {
        // 没有来源，直接返回失败
        if (from.length === 0) {
            return false;
        }
        // 有一个来源，加入任务
        if (from.length === 1) {
            this.addTask(Tasks.withdraw(from[0], RESOURCE_ENERGY));
            return true;
        }
        // 依次筛选
        for (let i = 0; i < from.length; ++i) {
            // truthy check
            if (!from[i]) {
                continue;
            }
            // 一些简单的检查
            let limit = 0;
            if (from[i].structureType === STRUCTURE_SPAWN) {
                limit = SPAWN_ENERGY_CAPACITY / 2;
            }
            else if (from[i].structureType === STRUCTURE_CONTAINER) {
                limit = CONTAINER_CAPACITY / 50 /* 60 */;
            }
            if (from[i].store[RESOURCE_ENERGY] > limit) {
                this.addTask(Tasks.withdraw(from[i], RESOURCE_ENERGY));
                return true;
            }
        }
        return false;
    }

}

assignPrototype(CreepExtension, Creep);